<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<title>指挥体系</title>
		<link rel="stylesheet" type="text/css" href="../css/api.css"/>
		<link rel="stylesheet" href="../lib/jquery.mobile-1.4.5/jquery.mobile-1.4.5.min.css">
		<script src="../lib/jquery.mobile-1.4.5/jquery.min.js"></script>
		<script src="../lib/jquery.mobile-1.4.5/jquery.mobile-1.4.5.min.js"></script>
		<script src="../lib/socket.io.js"></script>
		<script src="../lib/config.js"></script>
		<script src="../lib/common.js"></script>
		<style>
			.ui-field-contain {
				margin: 5px 10px;
			}
			table {
				width: 100%;
				border-spacing: 0;
			}
			th {
				text-align: left;
			}
			th h3 {
				margin: .6em 0 .6em .5em;
			}
			td .ui-btn {
				margin: .4em 0 .5em 0;
			}
			td .ui-btn-inner {
				padding: .4em 15px;
			}
		</style>
	</head>
	<body>
		<div style="margin:10px 0px;">
			<div class="ui-field-contain" style="">
				<label>会商任务:</label>
				<select name="taskList" id="taskList"></select>
			</div>
		</div>
		<div style="margin:10px 10px;">
			<label>聊天记录:</label>
		</div>
		<div id ='history' style="margin: 10px 10px;border: solid 1px #ddd;height:230px;overflow-y:auto;"></div>
		<div style="margin:0px 10px;clear:both;">
			<input id="tbContent" type="text" value="" data-clear-btn="true" style="height: 45px;">
		</div>
		<div style="margin: 15px 10px;float: left;">
			<a id="btnUploadImage"> <img src="../image/picture_add.png" style="width:32px;height: 32px;margin-right:15px;"/> </a>
			<a id="btnCamera"> <img src="../image/camera_add.png" style="width:32px;height: 32px;"/> </a>
		</div>
		<div style="margin: 0px 10px;float: left;" hidden="hidden">
			<a id="btnUsers"> <img src="../image/users_3.png" style="width:32px;height: 32px;"/> </a>
		</div>
		<div style="float:right;margin:0px 10px;">
			<input type="button" value="发送" id="btnSave" >
		</div>
		<div style="clear: both;"></div>
		<div id="divImages" style="margin:10px 10px;"></div>
	</body>
	<script type="text/javascript" src="../script/api.js"></script>
	<script type="text/javascript">
		var pics = [];
		var rwInfo = null;
		var rwRecords = [];
		apiready = function() {
			initialCamera();
			initial();
			//监听消息
			listenToSocketIO();
			//获取地理位置
			setInterval(reportLocation, 60000);
			$("#btnSave").click(function() {
				var content = $("#tbContent").val();
				//保存聊天记录
				var YJZHLTJLInfo = {
					"YJZHLTJL01010" : null,
					"YJZHLTJL01020" : rwInfo.YJCZRW01010,
					"YJZHLTJL01030" : getCookie("email"),
					"YJZHLTJL01040" : getCookie("display_name"),
					"YJZHLTJL01050" : getCookie("first_name"),
					"YJZHLTJL01060" : getCookie("last_name"),
					"YJZHLTJL01070" : getCookie("phone"),
					"YJZHLTJL01090" : '现场照片：' + getLocaleDatetime(),
					"YJZHLTJL01100" : 0
				};
				if (pics.length > 0)
					YJZHLTJLInfo.YJZHLTJL01100 = 1;
				if (content != '')
					YJZHLTJLInfo.YJZHLTJL01090 = content;
				api.showProgress({
					style : 'default',
					animationType : 'fade',
					title : '正在发送',
					text : '请稍等...',
					modal : false
				});
				YJZHLTJLInfo.YJZHLTJL01010 = Guid();
				var data = {
					"record" : [YJZHLTJLInfo]
				};
				addRecords("qcqf", "YJZHLTJL01", data, function(response) {
					//保存照片
					if (pics.length > 0) {
						uploadPictures(pics, YJZHLTJLInfo.YJZHLTJL01010, 'zhtxDoc');
						pics = [];
					}
					api.hideProgress();
					alert("保存成功！");
				}, showErro, false);
				api.hideProgress();
			});
		};
		function initial() {
			rwRecords = getMeetingTasks();
			if (rwRecords == null)
				return;
			for (var i = 0; i < rwRecords.length; i++) {
				var item = "<option value='" + rwRecords[i].YJCZRW01010 + "'>" + rwRecords[i].YJCZRW01020 + "</option>";
				$("#taskList").append(item);
			}
			$('#taskList')[0].selectedIndex = -1;
			$('#taskList').change(function() {
				var code = $(this).children('option:selected').val();
				for (var i = 0; i < rwRecords.length; i++) {
					if (rwRecords[i].YJCZRW01010 != code)
						continue;
					rwInfo = rwRecords[i];
					//显示任务基本信息
					var html = "灾害点名称：" + rwInfo.YJCZRW01060 + ",指挥中心：" + rwInfo.YJCZRW01150 + ",会商中心：" + rwInfo.YJCZRW01160 + ",灾害现场：" + rwInfo.YJCZRW01170;
					$("#history")[0].innerHTML += html;
					//加入应急指挥
					//查询任务记录表中是否有记录该人员加入该条任务
					var filter = "YJZHTX01020='" + rwInfo.YJCZRW01010 + "' and YJZHTX01030='" + getCookie("email") + "'";
					var joinPerson = null;
					getRecordsByFilter("qcqf", "YJZHTX01", filter, function(response) {
						if (response.record && response.record.length > 0) {
							joinPerson = response.record[0];
						}
					}, showErro, false);
					if (joinPerson != null) {
						joinPerson.YJZHTX01090 = 'C';
						joinPerson.YJZHTX01080 = null;
						joinPerson.YJZHTX01070 = getLocaleDatetime();
						updateRecords("qcqf", "YJZHTX01", joinPerson, function(response) {
							alert("加入成功");
						}, showErro, false, "YJZHTX01010");
						return;
					}
					joinPerson = {
						"YJZHTX01010" : Guid(),
						"YJZHTX01020" : rwInfo.YJCZRW01010,
						"YJZHTX01030" : getCookie("email"),
						"YJZHTX01040" : getCookie("display_name"),
						"YJZHTX01050" : getCookie("first_name"),
						"YJZHTX01060" : getCookie("last_name"),
						"YJZHTX01070" : getLocaleDatetime(),
						"YJZHTX01080" : null,
						"YJZHTX01090" : 'C',
						"YJZHTX01100" : null
					};
					var data = {
						"record" : [joinPerson]
					};
					addRecords("qcqf", "YJZHTX01", data, function() {
						alert("加入成功！");
					}, showErro, false);
				}
			})
		}

		function getMeetingTasks() {
			var record = null;
			var userName = getCookie("display_name");
			var filter = "YJCZRW01100 !='C' and YJCZRW01100 !='D' and YJCZRW01170 like '%" + userName + "%'";
			var order = 'YJCZRW01030 DESC';
			getRecordsByFilterOrder("qcqf", "YJCZRW01", filter, order, function(response) {
				if (response.record != null) {
					record = response.record;
				}
			}, showErro, false);
			return record;
		}

		function listenToSocketIO() {
			//ZhtxChat,指挥体系聊天记录
			try {
				var iosocket2 = io.connect(SocketIOServer + '/' + 'ZhtxChat');
				iosocket2.on('connect', function() {
					iosocket2.emit('subscribe', 'admin');
					//监听一个频道，频道名称为admin
					iosocket2.on('send', function(msg) {
						var num = msg.count;
						if (num > 0)
							refreshMessages();
					});
					iosocket2.on('disconnect', function() {
						iosocket2 = io.connect(SocketIOServer + '/' + 'ZhtxChat');
						//断开重连
					});
					iosocket2.on('error', function(data) {
						console.log('服务端抛出异常，异常信息:' + data.toString());
						//服务端抛出异常
					});
				});
			} catch (e) {
				console.log("SocketIO服务器连接失败，请刷新后重试！");
			}
		}

		//刷新群消息
		function refreshMessages() {
			if (rwInfo == null)
				return;
			var taskId = rwInfo.YJCZRW01010;
			var filter = "YJZHLTJL01020 ='" + taskId + "'";
			var order = "YJZHLTJL01080 DESC,YJZHLTJL01020"
			getPageRecordsByFilterOrder("qcqf", "YJZHLTJL01", filter, order, 50, 0, function(response) {
				if (response && response.record.length > 0) {
					$("#history")[0].innerHTML = '';
					var records = response.record;
					var html = '';
					for (var i = records.length - 1; i >= 0; i--) {
						var record = {
							guid : records[i].YJZHLTJL01010,
							name : records[i].YJZHLTJL01040,
							message : records[i].YJZHLTJL01090,
							time : records[i].YJZHLTJL01080.replace("T", " ").substr(0, 19),
							addCode : records[i].YJZHLTJL01100
						}
						html += "<div><span>" + record.name + " </span><span>" + record.time + "</span></div><div><p>" + record.message + "</p></div><br/>";
					}
					$("#history")[0].innerHTML += html;
					var div = document.getElementById("history");
					div.scrollTop = div.scrollHeight;
				}
			}, null, true);
		}

		//汇报现场位置
		function reportLocation() {
			console.log('获取设备所处位置');
			//获取设备所处位置
			var baiduLocation = api.require('baiduLocation');
			baiduLocation.startLocation({
				accuracy : '100m',
				filter : 1,
				autoStop : true
			}, function(ret, err) {
				var sta = ret.status;
				if (sta) {
					var str = '经度：' + ret.longitude + '<br>';
					str += '纬度：' + ret.latitude + '<br>';
					str += '更新时间：' + ret.timestamp + '<br>';
					console.log(str);
					saveLocation(ret.longitude, ret.latitude);
				} else
					console.log(err.msg);
			});
		}

		function saveLocation(lon, lat) {
			if (rwInfo == null || !rwInfo.YJCZRW01010)
				return;
			var item = {
				"YJDCRYWD01010" : Guid(),
				"YJDCRYWD01020" : rwInfo.YJCZRW01010,
				"YJDCRYWD01040" : lon,
				"YJDCRYWD01050" : lat,
				"YJDCRYWD01060" : getCookie("email"),
				"YJDCRYWD01070" : getCookie("display_name"),
				"YJDCRYWD01080" : getCookie("first_name"),
				"YJDCRYWD01090" : '灾害现场人员位置'
			};
			var data = {
				"record" : [item]
			};
			addRecords("qcqf", "YJDCRYWD01", data, function(response) {
				console.log("现场位置上报成功！");
			}, showErro, false);
		}

		function onPicBack(ret, err) {
			if (ret) {
				pics = [];
				pics.push(ret.data);
				var content = '现场照片-' + getLocaleTimeFileName();
				$("#tbContent").val(content);
			} else {
				api.alert({
					msg : err.msg
				});
			}
		}

		function initialCamera() {
			$("#btnUploadImage").click(function() {
				api.getPicture({
					sourceType : 'library',
					encodingType : 'jpg',
					mediaValue : 'pic',
					destinationType : 'url',
					allowEdit : false,
					quality : 50,
					targetWidth : 800,
					targetHeight : 800,
					saveToPhotoAlbum : false
				}, function(ret, err) {
					onPicBack(ret, err);
				});
			});
			$("#btnCamera").click(function() {
				api.getPicture({
					sourceType : 'camera',
					encodingType : 'jpg',
					mediaValue : 'pic'
				}, function(ret, err) {
					onPicBack(ret, err);
				});
			});
		}
	</script>
</html>